{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fd68b4f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09b0eec3",
   "metadata": {},
   "source": [
    "# 数组变形"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e49d184",
   "metadata": {},
   "source": [
    "使用reshape函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "74168017",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
       "       18, 19, 20])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.arange(1,21)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9b1674ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8e9536c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 改变数组形状，这里变成二维\n",
    "n2=numpy.reshape(a=n,newshape=(4,5))\n",
    "n2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3d203789",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a69df4d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 3,  4],\n",
       "       [ 5,  6],\n",
       "       [ 7,  8],\n",
       "       [ 9, 10],\n",
       "       [11, 12],\n",
       "       [13, 14],\n",
       "       [15, 16],\n",
       "       [17, 18],\n",
       "       [19, 20]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n2.reshape((-1,2)) # 这里-1表示任意剩余维度长度"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6aa452d9",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d804fab",
   "metadata": {},
   "source": [
    "# 级联合并"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48ac6813",
   "metadata": {},
   "source": [
    "concatenate()\n",
    "- 参数是列表或者元组\n",
    "- 级联的数组维度必须相同\n",
    "- 可通过axis参数改变级联的方向"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ecb1fef3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[63, 20,  8, 90, 81],\n",
       "       [39, 89, 79, 68, 66],\n",
       "       [20, 65, 28, 71, 31]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[18, 87, 85, 53, 97],\n",
       "       [35, 63, 77, 43, 62],\n",
       "       [50, 98, 13, 66, 99]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n1 = numpy.random.randint(1,100,size=(3,5))\n",
    "n2 = numpy.random.randint(1,100,size=(3,5))\n",
    "display(n1,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0adb75b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[63, 20,  8, 90, 81, 18, 87, 85, 53, 97],\n",
       "       [39, 89, 79, 68, 66, 35, 63, 77, 43, 62],\n",
       "       [20, 65, 28, 71, 31, 50, 98, 13, 66, 99]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 级联/合并\n",
    "numpy.concatenate((n1,n2)) # 可以看到这里默认是上下进行合并的\n",
    "numpy.concatenate((n1,n2),axis=0) # 这里axis是0表示第一个维度，也就是行，进行合并。\n",
    "numpy.concatenate((n1,n2),axis=1) # 1就表示第二个维度了，从第二个维度进行合并。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9541865",
   "metadata": {},
   "source": [
    "numpy.hstack和numpy.vstack\n",
    "- 水平级联和垂直级联"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "58822ec2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[63, 20,  8, 90, 81],\n",
       "       [39, 89, 79, 68, 66],\n",
       "       [20, 65, 28, 71, 31],\n",
       "       [18, 87, 85, 53, 97],\n",
       "       [35, 63, 77, 43, 62],\n",
       "       [50, 98, 13, 66, 99]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.hstack((n1,n2))\n",
    "numpy.vstack((n1,n2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81ed1791",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33fa9e63",
   "metadata": {},
   "source": [
    "# 切分/拆分/分割"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25a6b231",
   "metadata": {},
   "source": [
    "三个函数\n",
    "- numpy.split\n",
    "- numpy.hsplit\n",
    "- numpy.vsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "3063be17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4, 18, 28, 53],\n",
       "       [56, 31, 56, 21],\n",
       "       [25, 71, 57, 86],\n",
       "       [46, 42, 83,  9],\n",
       "       [35,  1, 46, 88],\n",
       "       [17, 46,  7, 96]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randint(0,100,size=(6,4))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "2c700d34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 4, 18, 28, 53],\n",
       "        [56, 31, 56, 21]]),\n",
       " array([[25, 71, 57, 86],\n",
       "        [46, 42, 83,  9]]),\n",
       " array([[35,  1, 46, 88],\n",
       "        [17, 46,  7, 96]])]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.vsplit(n,3) # 这里垂直拆分为三部分"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1b00aac",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50390a32",
   "metadata": {},
   "source": [
    "# 复制/拷贝"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc6c758d",
   "metadata": {},
   "source": [
    "copy()函数创建副本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "117ab047",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 赋值，用的是同一个内存\n",
    "n = numpy.arange(10)\n",
    "n2 = n\n",
    "n2[0] = 100\n",
    "display(n,n2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "a63c4a98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([100,   1,   2,   3,   4,   5,   6,   7,   8,   9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 拷贝，copy是深拷贝\n",
    "n1 = numpy.arange(10)\n",
    "n2 = n1.copy()\n",
    "n2[0]=100\n",
    "display(n1,n2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
